home *** CD-ROM | disk | FTP | other *** search
- unit singlebase;
-
- interface
-
- type
- TSingleton = class (TObject)
- public
- class function NewInstance: TObject; override;
- procedure FreeInstance; override;
- // debug
- class function ListClasses: string;
- class procedure FreeAll;
- end;
-
- implementation
-
- uses
- classes;
-
- var
- InstanceList: TStringList;
- Destroying: Boolean = False;
-
- class procedure TSingleton.FreeAll;
- var
- I: Integer;
- begin
- Destroying := True;
- for I := 0 to InstanceList.Count - 1 do
- InstanceList.Objects [I].Free;
- end;
-
- procedure TSingleton.FreeInstance;
- begin
- if Destroying then
- inherited FreeInstance;
- end;
-
- class function TSingleton.ListClasses: string;
- var
- I: Integer;
- begin
- Result := '';
- for I := 0 to InstanceList.Count - 1 do
- Result := Result + InstanceList [I] + ';';
- end;
-
- class function TSingleton.NewInstance: TObject;
- var
- idx: Integer;
- begin
- idx := InstanceList.IndexOf (ClassName);
- if idx >= 0 then
- Result := InstanceList.Objects [idx]
- else
- begin
- Result := inherited NewInstance;
- InstanceList.AddObject (ClassName, Result);
- end;
- end;
-
- initialization
- InstanceList := TStringList.Create;
-
- finalization
- TSingleton.FreeAll;
- InstanceList.Free;
-
- end.
-